<?php
namespace PsiFramework\Classes\Util\Security\DAO;

import("Classes.DAO.AbstractDAO");
import("Classes.Util.Security.Model.SecurityRole");

use \PsiFramework\Classes\DAO\AbstractDAO;
use \PsiFramework\Classes\Model\Object;
use \PsiFramework\Classes\Util\Manager\ApplicationManager;
use \PsiFramework\Classes\Util\Database\Filters\QueryFilter;
use \PsiFramework\Classes\Util\Database\Query\SqlColumn;
use \PsiFramework\Classes\Util\Database\Query\SqlFunction;
use \PsiFramework\Classes\Util\Database\Query\SqlJoin;
use \PsiFramework\Classes\Util\Database\Query\SqlOperation;
use \PsiFramework\Classes\Util\Database\Query\SqlTable;
use \PsiFramework\Classes\Util\Database\Query\SqlUsing;
use \PsiFramework\Classes\Util\Database\Query\SqlValue;
use \PsiFramework\Classes\Util\Security\Model\SecurityRole;

/**
 * PsiFramework
 * Php Simple Framework
 *
 * @author Rémi San
 * @version beta
 */

/**
 * Classe de DAO pour les droits
 *
 * @package Classes.Util.Security.DAO
 */
class SecurityRoleDAO extends AbstractDAO
{
     
    protected static $_singleton = null;

    /**
     * Returns the security roles for the user
     *
     * @param     User    $user    The user
     * @return    SecurityRole[]
     */
    public function getRolesForUser(Object $user)
    {
        $table = null ;
        $queryBuilder = $this->getSelectQueryBuilder($table);

        $applicationManager = ApplicationManager::getInstance();
        $application = $applicationManager->getApplication();
        $objParams = $application->getObjectParameter("SecurityUserRole");

        $sqlJoinClause = new SqlUsing();
        $sqlJoinClause->addIdentifier("role_id");

        $sqlTable = new SqlTable(
            $objParams->getTable(), $objParams->getSchema()
        );
        $sqlJoin = new SqlJoin($sqlTable, $sqlJoinClause);
        $queryBuilder->addJoin($sqlJoin);

        $colNom = new SqlColumn("user_id");
        $sqlValue = new SqlValue($user->getId());
        $equal = new SqlOperation(SqlOperation::$equal);
        $equal->addParameter($colNom);
        $equal->addParameter($sqlValue);

        $queryBuilder->addWhereClause($equal);

        return $this->processQueryBuilder($queryBuilder);
    }

    /**
     * Returns the security role for the specified name
     *
     * @param     string    $name    The name
     * @return    SecurityRole
     */
    public function getRoleByName(/*string*/ $name, $checkSecurity=true)
    {
        $colNom = new SqlColumn("role_name");
        $sqlValue = new SqlValue(mb_strtoupper($name, 'utf-8'));

        $upper = new SqlFunction("UPPER");
        $upper->addParameter($colNom);

        $equal = new SqlOperation(SqlOperation::$equal);
        $equal->addParameter($upper);
        $equal->addParameter($sqlValue);

        $roles = $this->retrieveAll(new QueryFilter($equal), null, $checkSecurity);
        if (!is_null($roles) && count($roles)>0) {
            return $roles[0] ;
        }
        return null ;
    }

    /**
     * Function to be called instead of the constructor.
     *
     * @return SecurityRoleDAO instance
     */
    public static function getInstance($objectParameters)
    {

        if (self::$_singleton==null) {
            self::$_singleton = new SecurityRoleDAO($objectParameters);
        }

        return self::$_singleton;
    }
}
